package kortmann.rene.leo_import;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import kortmann.rene.leo.factories.JsonObjectSet;
import kortmann.rene.leo.model.LEO;
import kortmann.rene.leo.service.Api;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class Main 
{
	/**
	 * @param args
	 */
	public static void main(String[] args) 
		throws Exception
	{
		Map<String, LEO> leos = new HashMap<>();
		
		InputStream inp = new FileInputStream("C:\\test\\Clubmitglieder.xls");
	    //InputStream inp = new FileInputStream("workbook.xlsx");

	    Workbook wb = WorkbookFactory.create(inp);
	    Sheet sheet = wb.getSheetAt(0);
	    
	    Iterator<Row> rows = sheet.iterator();
	    
	    Map<String, Integer> columns = new HashMap<>();
	    
	    Iterator<Cell> cells = rows.next().cellIterator();
	    
	    while(cells.hasNext())
	    {
	    	Cell cell = cells.next();
	    	
	    	columns.put(cell.getStringCellValue().toLowerCase().trim().replace(" ", "_").replace("(", "").replace(")", ""), cell.getColumnIndex());
	    }
	    
	    while(rows.hasNext())
	    {
	    	Row cur = rows.next();
	    	
	    	LEO leo = getLeoFromRow(cur, columns);
	    	
	    	String key = String.format("%s%s", leo.getNachname().toLowerCase().trim(), leo.getVorname().toLowerCase().trim());
	    	leos.put(key, leo);
	    }
		
	    Api api = new Api("rekor001", "isisisis");
	    
	    for(LEO cur : api.getAllLEOs())
	    {
	    	String key = String.format("%s%s", cur.getNachname().toLowerCase().trim(), cur.getVorname().toLowerCase().trim());
	    	
	    	if(leos.containsKey(key))
	    		leos.get(key).setId(cur.getId());
	    }
	    
	    Iterator<Entry<String, LEO>> it = leos.entrySet().iterator();
	    
	    while(it.hasNext())
	    {
	    	LEO cur = it.next().getValue();
	    	
	    	if(cur.getId() != null)
	    	{
	    		System.out.println(String.format("UPDATE %s: %s, %s", cur.getId(), cur.getNachname(), cur.getVorname()));
	    		api.updateLEO(cur);
	    	}
	    	else
	    	{
	    		System.out.println(String.format("CREATE: %s, %s", cur.getNachname(), cur.getVorname()));
	    		api.createLEO(cur);
	    	}
	    }
	}

	private static LEO getLeoFromRow(Row inRow, Map<String, Integer> inMap)
	{		
		LEO result = null;
		
		try
		{
			result = new LEO();
			
			for(Method curMethod : LEO.class.getMethods())
			{
				for(Annotation curAnno : curMethod.getAnnotations())
				{
					if(curAnno instanceof JsonObjectSet)
					{
						JsonObjectSet jo = (JsonObjectSet)curAnno;
						
						if(inMap.containsKey(jo.path()))
							curMethod.invoke(result, inRow.getCell(inMap.get(jo.path())).getStringCellValue());
					}
				}
			}			
		}
		catch(Exception ex)
		{
			result = null;
		}
		
		return result;
	}
}
